<?php
namespace Umpadmin\Controller;
use Think\Controller;
class EventController extends ExtendController {
	public function __construct(){
		parent::__construct();
		if(strtotime(session("logintime"))+1800<time()){
			session("cur_url",__SELF__);
			$this->relogin();
			die();
		}
	}
	public function relogin(){
		$this->assign("info","您已登陆超时，请重新登录后再继续操作");
		$this->display("Index:login");
	}
	public function setp0(){
		
	}
	public function step1_batch(){
		$this->step1();
	}
	public function step1(){
		$is_batch = I("batch");
		$catid = I("cat");
		$create = I("create");
		$template = $is_batch?"step1_batch":"";

		if($create){
			session("activity_id",null);
			session("activity_ids",null);
			session("pro_set",null);
		}
		$CAT = M("PromoCats");
		if($catid){
			session("create_in_cat",$catid);
			$pro_set = current($CAT->where("`sid`=".$this->sid." AND `id`='".$catid."'")->limit(1)->getField("`id`,`name`,`start_time`,`end_time`,`friend_num`,`decrease_amount`"));
		}else{
			$num = $CAT->where("`sid`=".$this->sid)->count();
			if($num>=1){
				$info="抱歉，当前版本只能创建一个活动。添加折扣请进入活动后点击“增加一个”按钮。<br />多活动版本正在开发中，请稍候。";
				$this->assign("info",$info);
				$this->display("only_one");
				die();
			}
			session("create_in_cat",null);
			$pro_set = session("pro_set");
		}
		$pro_set["decrease_amount"]=$pro_set["decrease_amount"]/100;
		$pro_set["once_pay"]=$pro_set["once_pay"]/100;
		$pro_set["item_num"]=count(explode($pro_set["item_iids"],","));
		$this->assign("activity_id",session("activity_id"));
		$this->assign("activity_ids",session("activity_ids"));
		$this->assign("pro_id",session("pro_id"));
		$this->assign("catid",$catid);
		$this->assign("pro_set",$pro_set);
		$this->display($template);
	}
	public function step2(){
		Vendor('taobaosdk.top.request.PromotionmiscItemActivityAddRequest');
		Vendor('taobaosdk.top.request.PromotionmiscItemActivityUpdateRequest');
		Vendor('taobaosdk.top.request.TmallPromotagTagFindRequest');
		Vendor('taobaosdk.top.request.TmallPromotagTagApplyRequest');
		$action = I("ac");
		$activity_id = I("activity_id")?I("activity_id"):session("activity_id");
		$pro_id = I("pro_id");
		$number = (int)I("number");
		$number = $number?$number:1;
		if($action=="save"){
			$data["sid"] = $this->sid;
			$data["name"] = I("active_name");
			$data["start_time"] = I("start_time");
			$data["end_time"] = I("end_time");
			$data["friend_num"] = I("friend_num");
			$data["decrease_amount"] = I("decrease_money")?I("decrease_money")*100:null;
			$data["promo_count"] = $number;
			if(session("create_in_cat")){
				$data["act_cat_id"] = session("create_in_cat");
			}else{
				$CAT = M("PromoCats");
				$cat = $CAT->where("`sid`=".$this->sid." AND `name`='".$data["name"]."'")->limit(1)->getField("`id`");
				if($cat){
					$data["act_cat_id"] = $cat;
					// $CAT->where("`id`=".$cat)->save($data);
				}else{
					$data["act_cat_id"] = $CAT->add($data);
				}
			}
			
			$data["activity_id"] = 0;
			$data["vip_user"] = I("vip_user")?I("vip_user"):null;
			$data["sum_pay"] = I("sum_pay")?I("sum_pay"):null;
			$data["pay_time_start"] = I("pay_time_start")?I("pay_time_start"):null;
			$data["pay_time_end"] = I("pay_time_end")?I("pay_time_end"):null;
			$data["item_iids"] = I("item_iids")?I("item_iids"):null;
			$data["once_pay"] = I("once_pay")?I("once_pay")*100:null;
			$data["is_decrease_money"] = 1;
			
			$active_name=$data["name"];
			$decrease_amount=$data["decrease_amount"];
			$friend_num=$data["friend_num"];
			for($i=1;$i<=$number;$i++){
				$data["name"]=$active_name.($i>1?$i:"");
				$data["decrease_amount"] = $decrease_amount*$i;
				$data["friend_num"] = $friend_num*$i;
				// echo "减钱".$data["decrease_amount"]."第".$i."个<br />";
				$req = new \TmallPromotagTagFindRequest;
				$req->setPageNo("1");
				$req->setPageSize("1");
				$req->setTagName($data["name"]);
				$tags = $this->c->execute($req, session("access_token"));
				if($tags->query_result->total_results>0){
					$data["user_tag_id"]=$tags->query_result->tag_list->promotion_tag[0]->tag_id;
				}else{
					$req = new \TmallPromotagTagApplyRequest;
					$req->setTagName($data["name"]);
					$req->setTagDesc("用于朋友圈的折扣标签");
					$req->setStartTime("2000-01-01 00:00:00");
					$req->setEndTime("2099-01-01 00:00:00");
					$tag = $this->c->execute($req, session("access_token"));
					if($tag->is_success){
						$data["user_tag_id"]=$tag->tag_id;
					}else{
						$tag_error = $tag;
					}
				}
				// echo $data["user_tag_id"];
				if($activity_id){
					$req = new \PromotionmiscItemActivityUpdateRequest;
					$req->setActivityId($activity_id);
					$req->setName($data["name"]);
					$req->setParticipateRange("1");
					$req->setStartTime($data["start_time"]);
					$req->setEndTime($data["end_time"]);
					$req->setIsUserTag("true");
					$req->setUserTag("NEW_".$data["user_tag_id"]);
					$req->setIsDecreaseMoney("true");
					$req->setDecreaseAmount($data["decrease_amount"]);
					$req->setIsDiscount("false");
					$req->setDiscountRate("800");
					$resp = $this->c->execute($req, session("access_token"));
					if($resp->is_success){
						$resp->activity_id = $activity_id;
						$resp->is_success = 1;
					}else{
						$tag_error = $resp;
					}
				}else{
					$req = new \PromotionmiscItemActivityAddRequest;
					$req->setName($data["name"]);
					$req->setParticipateRange("1");
					$req->setStartTime($data["start_time"]);
					$req->setEndTime($data["end_time"]);
					$req->setIsUserTag("true");
					$req->setUserTag("NEW_".$data["user_tag_id"]);
					$req->setIsDecreaseMoney("true");
					$req->setDecreaseAmount($data["decrease_amount"]);
					$req->setIsDiscount("false");
					$req->setDiscountRate("800");
					$resp = $this->c->execute($req, session("access_token"));
				}
				// print_r($resp);
				if($resp->activity_id && $resp->is_success==1){
					$data["activity_id"] = $resp->activity_id;
					$activity_ids[] = $resp->activity_id;
					session("activity_id",$resp->activity_id);
					$PROM = M("Promosions");
					$pro_id = session("pro_id".$resp->activity_id);
					if(!$pro_id){
						$pro_id = $PROM->add($data);
						session("pro_id".$resp->activity_id,$pro_id);
						if(session("create_in_cat")){
							$CAT = M("PromoCats");
							$CAT->where("`id`=".session("create_in_cat"))->setInc("promo_count");
						}
					}else{
						$PROM->where("`id`=".$pro_id)->save($data);
					}
				}else{
					$tag_error = $resp;
				}
			}
			session("activity_ids",$activity_ids);
			session("pro_set",$data);
		}
		$this->assign("error",$tag_error);
		$this->display();
	}
	
	public function step3(){
		Vendor('taobaosdk.top.request.PromotionmiscActivityRangeAddRequest');
		$item_iid = I("item_iid");
		$item_pic_url = I("item_pic_url");
		$item_title = I("item_title");
		$price = I("price");
		$activity_ids = session("activity_ids");
		for($i=0;$i<count($activity_ids);$i++){
			$ITEM = M("Items");
			foreach($item_iid as $k=>$iid){
				unset($data);
				$data["sid"]=$this->sid;
				$data["num_iid"]=$item_iid[$k];
				$data["title"]=$item_title[$k];
				$data["price"]=$price[$k]*100;
				$data["promo_id"]=session("pro_id".$activity_ids[$i]);
				// $data["activity_id"]=session("activity_id");
				$data["activity_id"]=$activity_ids[$i];
				$data["pic_url"]=$item_pic_url[$k];
				$data["id"] = $ITEM->add($data);
				$items[]=$data;
			}
			$req = new \PromotionmiscActivityRangeAddRequest;
			// $req->setActivityId(session("activity_id"));
			$req->setActivityId($activity_ids[$i]);
			$req->setIds(implode($item_iid,","));
			$resp = $this->c->execute($req, session("access_token"));
		}
		if(!$resp->is_success){
			$this->assign("error",$resp);
		}
		$this->assign("items",$items);
		$this->display();
	}
	public function step4(){
		$ids = I("id");
		$title = I("title");
		$recommend = I("recommend");
		$ITEM = M("Items");
		foreach($ids as $k=>$id){
			$data["id"]=$id;
			$data["title"]=$title[$k];
			$data["recommend"]=$recommend[$k];
			$ITEM->save($data);
		}
		$this->redirect('Index/index');
	}
}